package tools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.apache.commons.lang3.StringUtils;
import com.bj58.analysis.tools.etl.bean.ETLTrackBean;
import sourceTools.SourceEntry;
import sourceTools.TrackSourceType;

public class FormatTools{

	public static final String fullPathFormatReStr="^0(,\\d+){0,5}$";
	public static final Pattern fullPathFormatPattern = Pattern.compile(fullPathFormatReStr);
	//格式化fullpath
	public static String [] formatString(String fullPath){
		try{
			if(StringUtils.isBlank(fullPath)){
				return new String[]{"0"};
			}
			Matcher matc = fullPathFormatPattern.matcher(fullPath);
			if(!matc.find()){
				return new String[]{"0"};
			}
			String [] fullPathArr=fullPath.split(",");
			return fullPathArr;
		}
		catch(Exception e){
			return new String[]{"0"};
		}
	}
	public static String getBussiness(String[] cateidPath,Map<String,String> businessMap){
		String newStr = "other";
		for (int i = cateidPath.length-1; i >= 0 ; i--) {
			if(!cateidPath[i].equals("0") && businessMap.get(cateidPath[i])!=null){
				newStr = businessMap.get(cateidPath[i]);
				break;
			}
		}
		return newStr;
	}

	/**
	 * 根据id取出名字, 该方法只是适用于五八基础流量程序中需要 cityid以及对应的城市名称全路径
	* @param name
	* @param @return 设定文件
	* @return String DOM对象
	* @Exception 异常对象
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 */
	public static String getNamePath(String[] idpath,Map<String,String> idToNameMap,Map<String,String> idPathMap){
		String newStr = "";
		String newpath = "";
		for (int i = idpath.length-1; i >= 0 ; i--) {
			String id = idpath[i];
			if(!id.equals("0") && idToNameMap.get(id)!=null && idPathMap.get(id)!=null){
				newpath = idPathMap.get(id);
				break;
			}
		}
		for (String id : newpath.split(",")) {
			if(idToNameMap.get(id)!=null){
				newStr+=id+"###"+idToNameMap.get(id)+"\t";
			}
		}
		if(!newStr.equals("")){
			newStr = newStr.substring(0, newStr.length()-1);
		}
		return newStr;
	}
	/**
	 * 根据id取出名字
	* @param name
	* @param @return 设定文件
	* @return String DOM对象
	* @Exception 异常对象
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 */
	public static String getNewNamePath(String[] idpath,Map<String,String> idToNameMap,Map<String,String> idPathMap){
		String newStr = "";
		String newpath = "";
		for (int i = idpath.length-1; i >= 0 ; i--) {
			String id = idpath[i];
			if(!id.equals("0") && idToNameMap.get(id)!=null && idPathMap.get(id)!=null){
				newpath = idPathMap.get(id);
				break;
			}
		}
		for (String id : newpath.split(",")) {
			if(idToNameMap.get(id)!=null){
				newStr+=idToNameMap.get(id)+"\t";
			}
		}
		if(!newStr.equals("")){
			newStr = newStr.substring(0, newStr.length()-1);
		}
		return newStr;
	}
	public static String [] getPageTypeFullPath(String pageType){
		pageType=(pageType!=null&&!pageType.equals("")&&pageType.indexOf("%")==-1&&pageType.indexOf("/")==-1&&pageType.indexOf("\\")==-1)?pageType:"other";
		pageType=pageType.length()>20?"other":pageType;
		pageType=pageType.startsWith("list")?"list":(pageType.startsWith("detail")?"detail":pageType);

		pageType=pageType.replace("'","").replace("\"","").replace("\t","").replace("\n","");

		int flagList="jianli_list".equals(pageType)?1:0;
		int flagDetail="jianli_detail".equals(pageType)?1:0;

		if(flagList==1){
			return new String[]{"all","listanddetail","jianli_list","list"};
		}
		if(flagDetail==1){
			return new String[]{"all","listanddetail","jianli_detail","detail"};
		}

		pageType= pageType.startsWith("list") ?"list": (pageType.startsWith("detail") ?"detail":pageType);

		return (pageType.equals("detail") || pageType.equals("list") )?new String[]{"all","listanddetail",pageType}:new String [] {"all",pageType};
	}

	public static List<String> getSourceFullPath(ETLTrackBean trackinfo,String platform,int dept){
		List<String> visitSourceList = new ArrayList<String>();
		if(platform.equals("pc")){
			String sourceStr = TrackSourceType.getSourceType(trackinfo ,Dict.pcsourceList ,Dict.marketSpmList,Dict.pcsourceLMList);
			visitSourceList = FormatTools.getFullPathList(sourceStr, dept);
		}else{
			List<String> list = SourceEntry.getSourceList2(trackinfo, Dict.msourceQDMap, Dict.msourceLMMap,Dict.marketSpmList);
			String sourceStr = "";
			for (String str : list) {
				if(!str.equals("all")){
					sourceStr += str+"\t";
				}
			}
			if(sourceStr.equals("")){
				visitSourceList = FormatTools.getFullPathList("", dept);
			}else{
				visitSourceList = FormatTools.getFullPathList(sourceStr.substring(0, sourceStr.length()-1), dept);
			}
		}
		return visitSourceList;
	}

	public static List<String> getNewFullPath(List<String> fullPath){
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < fullPath.size(); i++) {
			String line = fullPath.get(i);
			if(i==0){
				list.add("0"+"###"+line);
			}else{
				String id = line.split(",",-1)[i-1].split("###",-1)[0];
				String newStr = "";
				for (String str : line.split(",",-1)) {
					if(str.split("###",-1).length>1){
						newStr+=str.split("###",-1)[1]+",";
					}else{
						newStr+=str+",";
					}
				}
				list.add(id+"###"+newStr.substring(0, newStr.length()-1));

			}
		}

		return list;
	}
	/**
	 * 向上汇总组合 dept 级别
	* @auther caods
	* @param name
	* @param @return 设定文件
	* @return String DOM对象
	* @Exception 异常对象
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 */
	public static List<String> getFullPathList(String sourceStr,int dept){
		List<String> newlist = new ArrayList<String>();
		String allstr = "";
		for (int i = 0; i < dept; i++) {
			allstr+="all,";
		}
		newlist.add(allstr.substring(0, allstr.length()-1));
		if(null == sourceStr || sourceStr.equals("")){
			return newlist;
		}
		List<String> visitSourceList = new ArrayList<String>();
		for (String source : sourceStr.split("\t")) {
			visitSourceList.add(source);
		}
		for (int i = 0; i < visitSourceList.size()&&i<dept; i++) {
			String k1 = "";
			for (int j = 0; j <= i; j++) {
				k1+=visitSourceList.get(j)+",";
			}
			String k2 = "";
			for (int j = i; j < dept-1; j++) {
				k2+="all,";
			}

			newlist.add((k1+k2).substring(0, (k1+k2).length()-1));
		}
		return newlist;
	}

	public static void main(String[] args)
	{
		List<String> list = getFullPathList("qd\t360浏览器", 4);
		for (String string : list) {
			System.out.println(string);
		}
		System.out.println("----------------------------------------------");
//		List<String> list2 = getNewFullPath(list);
//		for (String string : list2) {
//			System.out.println(string);
//		}
	}

}
